package com.mall.shopping.services;

import com.alibaba.fastjson.JSON;
import com.mall.shopping.IContentService;
import com.mall.shopping.constant.GlobalConstants;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dto.NavListResponse;
import com.mall.shopping.dto.PanelContentDto;
import com.mall.shopping.dto.PanelDto;
import com.mall.shopping.services.cache.CacheManager;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import com.mall.user.constants.SysRetCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author : L
 * @ProjectName : pepsi
 * @Date : 1/24/2021
 * @Description : 导航栏显示
 */
@Service
@Component
@Slf4j
public class IContentServiceImpl implements IContentService {

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    CacheManager cacheManager;

    @Override
    public NavListResponse queryNavList() {

        NavListResponse response = new NavListResponse();
        response.setCode(ShoppingRetCode.SUCCESS.getCode());
        response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        try {
            String json = cacheManager.checkCache(GlobalConstants.HEADER_PANEL_CACHE_KEY);
            if(StringUtils.isNoneEmpty(json)){
                //缓存不为空,说明缓存里有这样的数据
                List<PanelContentDto> panelContentDtos = JSON.parseArray(json, PanelContentDto.class);
                response.setPannelContentDtos(panelContentDtos);
                return response;
            }

            Example exampleContent = new Example(PanelContent.class);
            exampleContent.setOrderByClause("sort_order");
            Example.Criteria criteriaContent = exampleContent.createCriteria();
            criteriaContent.andEqualTo("panelId", GlobalConstants.HEADER_PANEL_ID);
            List<PanelContent> pannelContents = panelContentMapper.selectByExample(exampleContent);
            List<PanelContentDto> panelContentDtos = contentConverter.panelContents2Dto(pannelContents);
            //添加缓存操作
            cacheManager.setCache(GlobalConstants.HEADER_PANEL_CACHE_KEY,
                    JSON.toJSONString(panelContentDtos),
                    GlobalConstants.HOMEPAGE_EXPIRE_TIME);

            response.setPannelContentDtos(panelContentDtos);

        }catch (Exception e){
            log.error("ContentServiceImpl.queryNavList Occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
//        Example example = new Example(PanelContent.class);
//        example.createCriteria().andEqualTo("panelId", 0);
//        List<PanelContent> panelContents = panelContentMapper.selectByExample(example);
//        List<PanelContentDto> panelContentDtos = contentConverter.panelContents2Dto(panelContents);
//
//        navListResponse.setPannelContentDtos(panelContentDtos);
//        navListResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
//        navListResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
//        return navListResponse;
    }
}
